
<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">名字</label>
        <el-input v-model="query.name" clearable placeholder="名字" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">礼帐类型</label>
        <el-input v-model="query.type" clearable placeholder="礼帐类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">方式</label>
        <el-input v-model="query.way" clearable placeholder="礼帐类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">关系</label>
        <el-input v-model="query.relationship" clearable placeholder="礼帐类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">是否已还</label>
        <el-input v-model="query.isReturn" clearable placeholder="礼帐类型" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <!-- <date-range-picker v-model="query.date" start-placeholder="dateStart" end-placeholder="dateStart" class="date-item" /> -->
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="名字">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="礼帐金额">
            <el-input v-model="form.otherContent" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="礼帐类型">
            <el-input v-model="form.type" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="时间">
            <el-input v-model="form.date" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="方式">
            <el-input v-model="form.way" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="关系">
            <el-input v-model="form.relationship" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="是否已还">
            <el-input v-model="form.isReturn" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :summary-method="getSummaries"
        show-summary
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名字" />
        <el-table-column prop="otherContent" label="礼帐金额" />
        <el-table-column prop="type" label="礼帐类型" />
        <el-table-column prop="date" label="时间" />
        <el-table-column prop="way" label="方式" />
        <el-table-column prop="relationship" label="关系" />
        <el-table-column prop="isReturn" label="是否已还" />
        <el-table-column v-if="checkPer(['admin','giftAccount:edit','giftAccount:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGiftAccount from '@/api/giftAccount'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, type: null, date: null, way: null, relationship: null, isReturn: null, otherContent: null }
export default {
  name: 'GiftAccount',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'giftAccount', url: 'api/giftAccount', idField: 'id', sort: 'id,desc', crudMethod: { ...crudGiftAccount }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'giftAccount:add'],
        edit: ['admin', 'giftAccount:edit'],
        del: ['admin', 'giftAccount:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'name', display_name: '名字' },
        { key: 'type', display_name: '礼帐类型' }
      ]
    }
  },
  methods: {
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总价'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ' 元'
        } else {
          sums[index] = '-----'
        }
      })

      return sums
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
